#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
#include <fastdds/dds/domain/DomainParticipant.hpp>

#include <fastdds/dds/subscriber/Subscriber.hpp>

#include "HelloWorldPubSubTypes.h"
#include <unistd.h>

using namespace eprosima::fastdds::dds;

class HelloWorldSubscriber
{
public:
    HelloWorldSubscriber()
        : type_(new HelloWorldPubSubType)
    {
    }
    ~HelloWorldSubscriber()
    {
        if (reader_)
        {
            subscriber_->delete_datareader(reader_);
        }
        if (subscriber_)
        {
            participant_->delete_subscriber(subscriber_);
        }
        if (topic_)
        {
            participant_->delete_topic(topic_);
        }
        if (participant_)
        {
            DomainParticipantFactory::get_instance()->delete_participant(participant_);
        }
    }
    bool init()
    {
        DomainParticipantQos participantQos;
        participantQos.name("participant_subscriber");
        participant_ = DomainParticipantFactory::get_instance()->create_participant(0, participantQos);
        if (participant_ == nullptr)
        {
            std::cout << "create_participant() failed" << std::endl;
            return false;
        }

        type_.register_type(participant_);
        topic_ = participant_->create_topic("HelloWorldTopic", "HelloWorld", TOPIC_QOS_DEFAULT);
        if (topic_ == nullptr)
        {
            std::cout << "create_topic() failed!" << std::endl;
            return false;
        }

        return true;
    }

private:
    DomainParticipant *participant_;
    Subscriber *subscriber_;
    DataReader *reader_;
    Topic *topic_;
    TypeSupport type_;
};

int main()
{
    HelloWorldSubscriber mysub;
    mysub.init();
    while (true)
    {
        sleep(1);
    }
    return 0;
}